#!/bin/bash

function start
{
    check_proc_exist "${PROC_NAME}" "${PROC_PORT}"
    RET=$?
    if [ ${RET} == ${RET_OK} ];then
        RET_ERR_MSG="${PROC_NAME} is already started."
        return ${RET_OK}
    fi

    if [ ! -d ${BIN_DIR} ];then
        RET_ERR_MSG="The forder \"${BIN_DIR}\" is not exist."
        return ${RET_ERR}
    elif [ ! -f ${BIN_DIR}/${PROC_NAME} ];then
        RET_ERR_MSG="The file \"${BIN_DIR}/${PROC_NAME}\" is not exist."
        return ${RET_ERR}
    elif [ ! -x ${BIN_DIR}/${PROC_NAME} ];then
        RET_ERR_MSG="The file \"${BIN_DIR}/${PROC_NAME}\" cant execute."
        return ${RET_ERR}
    elif [ ! -d ${STA_DIR} -o ! -d ${CNF_DIR} ];then
        RET_ERR_MSG="The forder \"${STA_DIR}\" is not exist."
        return ${RET_ERR}
    elif [ ! -f ${CNF_DIR}/${PROC_CONF} ];then
        RET_ERR_MSG="The file \"${CNF_DIR}/${PROC_CONF}\" is not exist."
        return ${RET_ERR}
    elif [ ! -d ${LOG_DIR} ];then
        RET_ERR_MSG="The forder \"${LOG_DIR}\" is not exist."
        return ${RET_ERR}
    fi

    ${START_COMMAND} &>${LOG_DIR}/${CONTROL_LOG} &
    RET=$?
    cd - 1>/dev/null

    if [ ${RET} != ${RET_OK} ];then
        RET_ERR_MSG="Start failed."
        return ${RET_ERR}
    fi

    WAIT_TIME=0
    while [ 1 ]
    do
        if [ ${WAIT_TIME} -gt ${MAX_HEC_TME} ];then
            RET_ERR_MSG="Time out, start failed."
            return ${RET_ERR}
        fi
        ((WAIT_TIME++))

        healthcheck;
        RET=$?
        
        if [ ${RET} == ${RET_ERR} ];then
            sleep 1
            continue
        elif [ ${RET} == ${RET_OK} ];then
            RET_ERR_MSG="Start successfully."
            return ${RET_OK}
        fi
    done
}

function stop
{
    check_proc_exist "${PROC_NAME}" "${PROC_PORT}"
    RET=$?
    if [ ${RET} != ${RET_OK} ];then
        RET_ERR_MSG="${PROC_NAME} is not started or not listen on port[${PROC_PORT}]"
        return ${RET_ERR}
    fi
    
    ${STOP_COMMAND}
    sleep ${MAX_WAT_TME}

    check_proc_exist "${PROC_NAME}" "${PROC_PORT}"
    RET=$?
    if [ ${RET} == ${RET_OK} ];then
        RET_ERR_MSG="Stop failed."
        return ${RET_ERR}
    fi
    
    RET_ERR_MSG="Stop successfully."
    return ${AOS_OK} 
}

function restart
{
    stop;
    RET=$?
    if [ ${RET} != ${RET_OK} ];then
        return ${RET_ERR}
    fi

    start;
    RET=$?
    
    if [ ${RET} == ${RET_OK} ];then
        RET_ERR_MSG="Restart successfully."
        return ${RET_OK}
    else
        RET_ERR_MSG="Restart failed."
        return ${RET_ERR}
    fi
}

function healthcheck
{
    if [ "${CHK_PROC_EXIST}" == "1" ];then
        check_proc_exist "${PROC_NAME}" "${PROC_PORT}"
        RET=$?
        if [ ${RET} != ${RET_OK} ];then
            RET_ERR_MSG="${PROC_NAME} is not started."
            return ${RET_ERR}
        fi
    fi

    if [ "${CHK_PROC_NUM}" == "1" ];then
        check_proc_num "${PROC_NAME}" "${TRD_NUM}"
        RET=$?
        if [ ${RET} != ${RET_OK} ];then
            RET_ERR_MSG="Thead count is little then ${TRD_NUM}."
            return ${RET_ERR}
        fi
    fi

    if [ "${CHK_LOG_ROLL}" == "1" ];then
        check_log_roll "${LOG_DIR}/${PROC_LOG}" ${LOG_CHK_TME}
        RET=$?
        if [ ${RET} != ${RET_OK} ];then
            RET_ERR_MSG="${LOG_DIR}/${PROC_LOG} is not roll."
            return ${RET_ERR}
        fi
    fi

    return ${RET_OK}
}
